// 单独提出来 因为 middleware 会引用，所以不能放在 auth。js 里
export const authConfig = {
  pages: {
    signIn: "/login", // 自定义登录页
  },
  providers: [], //
  callbacks: {
    async jwt({ token, user }) {
      if (user) {
        token.id = user.id;
        token.isAdmin = user.isAdmin;
      }
      return token;
    },
    async session({ session, token }) {
      if (token) {
        session.user.id = token.id;
        session.user.isAdmin = token.isAdmin;
      }
      return session;
    },
    authorized({ auth, request }) {
      const { user } = auth || {};
      const isOnAdmin = request.nextUrl.pathname.startsWith("/admin");
      const isOnBlog = request.nextUrl.pathname.startsWith("/blog");
      const isOnLogin = request.nextUrl.pathname.startsWith("/login");
      if (isOnAdmin && !user?.isAdmin) {
        return false;
      }
      if (isOnBlog && !user) {
        return false;
      }

      // if (isOnLogin && user) {
      //   return Response.redirect(
      //     new URL(
      //       request.nextUrl.searchParams.get("callbackUrl") || "/",
      //       request.nextUrl
      //     )
      //   );
      // }
      return true;
    },
    // redirect({ url, baseUrl }) {
    //   console.log(new URL(url));
    //   // if (url.startsWith("/")) return `${baseUrl}${url}`;
    //   // // Allows callback URLs on the same origin
    //   // if (new URL(url).origin === baseUrl) return url;
    //   return baseUrl;
    // },
  },
};
